<template>
  <div class="AddServiceTime">
    <div class="ele-title"><h4>添加服务时间模板</h4></div>
    <div class="ele-con-res"><span>填写模板信息</span></div>
    <div class="ele-content">
      <el-form ref="form" :model="form" label-width="100px" label-position="left" :rules="timeRules">
        <el-form-item label="模板名称:" prop="modelName" style="margin-top: 20px">
          <el-col :span="6">
            <el-input v-model="form.modelName" placeholder="填写模板名称" />
          </el-col>
        </el-form-item>
        <el-form-item label="可预约时间:" prop="day">
          <el-col :span="6">
            <el-input-number v-model="form.day" :precision="0" :controls="false" />
          </el-col>
          <el-col :span="2">天</el-col>
          <!--          <el-col :span="20">
            <p class="title-txt">用户可预约的最长天数，租赁商品的附加规格从用户收货后开始计算。</p>
          </el-col>-->
        </el-form-item>
        <el-form-item>
          <el-col :span="24">
            <el-alert
              title="用户可预约的最长天数，租赁商品的附加规格从用户收货后开始计算。"
              type="info"
              :closable="false"
            />
          </el-col>
        </el-form-item>
        <div v-for="(item,index) in list" :key="index">
          <DoctorTimeInterval ref="DoctorTimeInterval" :index="index" :time-form="item" @delete="deleteTimeInterval" />
        </div>
        <el-form-item>
          <el-col :span="6">
            <el-button type="text" @click="addDate">添加时间段</el-button>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-col :span="6" class="el-col-offset-2">
            <el-button type="primary" @click="submitTimeModel">提交</el-button>
          </el-col>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getShopID } from '@/utils/auth'
import DoctorTimeInterval from '@/views/service/component/DoctorTimeInterval'
import { addServiceTime } from '@/api/service'

export default {
  name: 'AddServiceTime',
  components: { DoctorTimeInterval },
  data() {
    return {
      list: [],
      form: {
        modelName: '',
        day: ''
      },
      timeRules: {
        modelName: [
          { required: true, message: '请填写模板名称', trigger: 'blur' }
        ],
        day: [
          { required: true, message: '请填写可预约天数', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    addDate() {
      this.list.push(1)
    },
    deleteTimeInterval(index) {
      this.list.splice(index, 1)
    },
    submitTimeModel() {
      this.$refs.form.validate(v => {
        if (v) {
          if (!this.$refs.DoctorTimeInterval.validate()) {
            return
          }
          const timeList = []
          this.$refs.DoctorTimeInterval.forEach(item => {
            const timeString = {
              startTime: item.getData().startTime,
              endTime: item.getData().endTime,
              people: item.getData().people
            }
            timeList.push(timeString)
          })
          const data = {
            shopId: getShopID(),
            timeModelName: this.form.modelName,
            appointDay: this.form.day,
            timeList: timeList
          }
          console.log(data)
          addServiceTime(data).then(response => {
            console.log(response.data)
            this.$message({ type: 'success', message: '时间模板添加成功' })
            this.$store.dispatch('tagsView/delView', this.$route)
            this.$router.go(-1)
          })
        }
      })
    },
    handleRomeve(file) {
      console.log(file)
    }
  }
}
</script>

<style lang="scss" scoped>
.AddServiceTime {
  padding: 0px 100px;
  .ele-title {
    margin-top: 40px;
    padding-left: 20px;
  }
  .ele-con-res {
    background: #f3f3f3;
    height: 50px;
    border-radius: 5px;
    line-height: 50px;
    margin-top: 40px;
    span {
      font-size: 16px;
      font-weight: bold;
      padding-left: 20px;
    }
  }
  .ele-content {
    padding: 0px 25px;
    /*::v-deep.el-form-item--medium {
      margin-top: 20px;
    }*/
    ::v-deep.el-form-item__label {
      width: 100px;
      text-align: start;
    }
    .ele-proportion,
    .ele-pre {
      margin-left: 100px;
    }
    .ele-pre {
      line-height: 30px;
      font-size: 14px;
      color: gray;
      width: 520px;
    }
    .title-txt {
      font-size: 14px;
      color: gray;
      white-space: nowrap;
      margin: 0;
      padding: 0;
    }
    .line{
      margin-left: 38px;
      height: 36px;
      line-height: 36px;
      text-align: center;
    }

  }
}
.flex {
  display: flex;
}
.marginleft {
  margin-left: 12px;
}
.text {
  text-align: end;
  padding-right: 10px;
}
</style>
